package net.siufung.market.base.permit.controller.regulation;

import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.permit.controller.model.dto.RoleSaveDto;
import net.siufung.market.base.permit.mapper.IRoleMapper;

/**
 * @author 陈建峰
 * @since 2022/4/18 2:09 下午
 */
@AllArgsConstructor
public class RoleCreateRegulation extends AbstractRegulation<RoleSaveDto> {

    private static final long serialVersionUID = -5163931575363019248L;

    private final IRoleMapper roleMapper;

    @Override
    public boolean isSatisfiedBy(RoleSaveDto roleSaveDto) {
        VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
        verifyRepeatDto.setMethodEventEnum(MethodEventEnum.ADD);
        verifyRepeatDto.setPk(null);
        verifyRepeatDto.setVerifyKeyValue(roleSaveDto.getRoleName());
        if(!roleMapper.checkRoleNameNotRepeat(verifyRepeatDto)){
            throw new BizServiceException("角色名称重复！");
        }
        verifyRepeatDto.setVerifyKeyValue(roleSaveDto.getRoleCode());
        if(!roleMapper.checkRoleCodeNotRepeat(verifyRepeatDto)){
            throw new BizServiceException("角色编码重复！");
        }
        return Boolean.TRUE;
    }

}
